﻿using Ewell.BizComponent.Charge.Model;
using Ewell.BizComponent.Service.Server;
using Ewell.Interface.Accounting.Common.Model;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Proxy.Model;
using Ewell.Interface.ReadCard.Common.Model.Response;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Ewell.BizComponent.Charge.UI.VM
{
    public class SearchComponentVm:NotifyPropertyChanged
    {
        /// <summary>
        /// 检索按钮
        /// </summary>
        private List<ButtonComponentVm> _buttonComponents;
        public List<ButtonComponentVm> buttonComponents { get { return _buttonComponents; } set
            {
                if (_buttonComponents != value)
                {
                    _buttonComponents = value;
                    OnPropertyChanged(nameof(buttonComponents));
                }
            }
        }
        /// <summary>
        /// 检索文本框
        /// </summary>
        private string _searchText;
        public string searchText { get
            {
                return _searchText;
            } set { 
                if(value != _searchText)
                {
                    _searchText = value;
                    OnPropertyChanged(nameof(searchText));
                }
            }
        }
        /// <summary>
        /// 收费上下文
        /// </summary>
        public CashierContext context { get; set; }
        public string currBizTradeCode { get; set; }

        public ICommand readCardCmd => new RelayCommand(param =>
        {
            var buttonComponent=this.buttonComponents.FirstOrDefault(f => f.isChecked);
            if(buttonComponent != null&& buttonComponent.triggerTypeCoding == "2")
            {
                ReadCardServer readCardServer = new ReadCardServer();
                ReadCardResponse readCardResponse= readCardServer.readCard(new ReadCardRequest()
                {
                    searchTypeCoding = buttonComponent.bizSearchTypeCoding,
                    searchValue= searchText,
                    currBizTradeCode=currBizTradeCode,
                    interfaceId= buttonComponent.interfaceId.ToString(),
                    uuheader=JsonConvert.SerializeObject(LoginInfo.loginUser),
                });

                context.basicInfo = JsonConvert.DeserializeObject<BasicInfo>(JsonConvert.SerializeObject(readCardResponse));
            }
        });
    }
}
